package com.sie.framework.module;

import android.graphics.Bitmap;
import android.os.SystemClock;
import android.util.Log;

import com.sie.framework.appinterface.APDU_RESP;
import com.sie.framework.bean.IdentityBean;
import com.sie.framework.channel.ChannelInstance;
import com.sie.framework.constconfig.ErrorDefine;
import com.sie.framework.listen.IdentityListen;
import com.sie.tools.ByteTool;
import com.sie.tools.LogUtil;
import com.sie.tools.SingletonThreadPool;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.concurrent.ExecutorService;

import com.sabirjan.reader.tools.Tool;

/**
 * Created by zhq on 2015/12/10.
 */
public class IdentityModule extends CommonModule {
	private IdentityBean bean;
	private final static String TAG = "IdentityModule";
	private byte[] recvBuf = new byte[1024];
	private static IdentityModule module = null;
	private ReadCardThread VaIDCard_Thread = null;
	private IdentityListen callback = null;
	private static ExecutorService threadPool = SingletonThreadPool
			.getInstance();
	private boolean lockFlag = false;

	private IdentityModule() {

	}

	@Override
	public boolean isFinish() {
		if (VaIDCard_Thread == null || VaIDCard_Thread.isThreadFinished()) {
			return true;
		} else {
			return false;
		}
	}

	public void setCallback(IdentityListen callback) {
		this.callback = callback;
	}

	public static IdentityModule getInstance() {
		if (module == null) {
			module = new IdentityModule();
		}
		return module;
	}

	public void startRead(final boolean withFinger) {
		Log.e(TAG, "startRead");
		Runnable r = new Runnable() {
			@Override
			public void run() {
				if (null != VaIDCard_Thread
						&& !VaIDCard_Thread.isThreadFinished()) {
					VaIDCard_Thread.setFlag(false);
					while (!VaIDCard_Thread.isThreadFinished()) {
						try {
							Thread.sleep(10);
						} catch (InterruptedException e) {
							e.printStackTrace();
						}
					}
				}
				while (lockFlag) {
					try {
						Thread.sleep(1);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
				VaIDCard_Thread = null;
				bean = new IdentityBean();
				VaIDCard_Thread = new ReadCardThread();
				VaIDCard_Thread.setWithFinger(withFinger);
				VaIDCard_Thread.start();
			}
		};
		threadPool.submit(r);
	}

	@Override
	public int OpenModule() {
		int ret = 0;
		for (int p = 0; p < 5; p++) {
			ret = api.IDCard_Open();
			if (ret == 0) {
				break;
			} else {
				
				api.IDCard_Close();
				ChannelInstance.resetChannel();
				SystemClock.sleep(100);
			}
		}
		return ret;
	}

	@Override
	public void closeModule(boolean isConnected) {
		Log.e(TAG, "closeModule");
		if (null != VaIDCard_Thread && !VaIDCard_Thread.isThreadFinished()) {
			VaIDCard_Thread.setFlag(false);
//			while (!VaIDCard_Thread.isThreadFinished()) {
//				try {
//					Thread.sleep(10);
//				} catch (InterruptedException e) {
//					e.printStackTrace();
//				}
//			}
		}
		if (isConnected) {
			api.IDCard_Close();
		}

		lockFlag = false;
	}

	public class ReadCardThread extends Thread {
		private boolean m_bThreadFinished = false;
		public String m_strName = "";
		public String m_strGender = "";
		public String m_strNation = "";
		public String m_strBornDate = "";
		public String m_strAddress = "";
		public String m_strNumber = "";
		public String m_strAuthor = "";
		public String m_strPeriodFrom = "";
		public String m_strPeriodTo = "";
		private boolean runFlag = true;
		private boolean withFinger = false;

		public void setWithFinger(boolean withFinger) {
			this.withFinger = withFinger;
		}

		public boolean isWithFinger() {
			return withFinger;
		}

		public boolean isThreadFinished() {
			return m_bThreadFinished;
		}

		public ReadCardThread() {
			runFlag = true;
		}

		public void setFlag(boolean flag) {
			runFlag = flag;
		}

		@Override
		public void run() {
			try {
				int ret = 1;
				ret = OpenModule();
				if (ret != 0) {
					if (callback != null) {
						callback.onReadFail(ErrorDefine.ERR_MODULE_ID_CARD,
								"身份证模块异常");
					}
					api.IDCard_Close();
					m_bThreadFinished = true;
					return;
				}
				Log.d("ReadCardThread[ run ]", "run() begin");
				if (callback != null) {
					callback.onStart();
				}
				int i = 0;
				while (runFlag) {
					synchronized (this) {
						APDU_RESP ApduResp = new APDU_RESP();

						try {
							ret = api.IDCard_Test(ApduResp);
						} catch (ArrayIndexOutOfBoundsException e) {
							Log.e(TAG, e.getMessage());
							// 失败跳过再读取
							continue;
						}
						Log.e(TAG, ret + "!!");
						if (ret == 0) {
							
							Log.e(TAG, "读出的原始数据：" + ByteTool.bytearrayToHexString(ApduResp.DataOut, ApduResp.DataOut.length));
							String tmpstr = "";
							int myloopi, unicode;

							String cardType = new String(ApduResp.DataOut, 248,
									2, "UTF-16LE").trim();// 证件类型

							if (cardType.equals("I")) {

								String enName = new String(ApduResp.DataOut, 0,
										120, "UTF-16LE").trim();

								String fprSex = new String(ApduResp.DataOut,
										120, 2, "UTF-16LE").trim();

								String frpIDNo = new String(ApduResp.DataOut,
										122, 30, "UTF-16LE").trim();

								String countryCode = new String(
										ApduResp.DataOut, 152, 6, "UTF-16LE")
										.trim();

								String CHNName = new String(ApduResp.DataOut,
										158, 30, "UTF-16LE").trim();

								String validStart = new String(
										ApduResp.DataOut, 188, 16, "UTF-16LE")
										.trim();

								String validEnd = new String(ApduResp.DataOut,
										204, 16, "UTF-16LE").trim();

								String Birthday = new String(ApduResp.DataOut,
										220, 16, "UTF-16LE").trim();

								String idVer = new String(ApduResp.DataOut,
										236, 4, "UTF-16LE").trim();

								String issuingAuthorityCode = new String(
										ApduResp.DataOut, 240, 8, "UTF-16LE")
										.trim();
								 
								System.arraycopy(ApduResp.DataOut, 256, recvBuf, 0, 1024);
								
								bean.fullName = CHNName;
								bean.icon = decodePhoto(recvBuf);
								bean.birthday = Birthday;
								bean.idNo = frpIDNo;
								bean.idOrg = issuingAuthorityCode;
								bean.beginDate = validStart;
								bean.endDate = validEnd;
								// bean.idAddr = m_strAddress;
								bean.countryCode= countryCode;
								bean.nation = countryMap.get(countryCode);
//								bean.sex = findSexZW(Integer.valueOf(fprSex));
								try {
									int sex_num = 9;
									if(fprSex != null && fprSex.trim().length() < 2){
										sex_num = Integer.parseInt(fprSex);
									}
									bean.sex = findSexZW(sex_num);
								} catch (NumberFormatException e) {
									// 失败跳过再读取
									continue;

								}
								
								bean.cardType = "1";
								bean.cardVer = idVer;
								bean.enName = enName;

							}else if(cardType.equals("J")){

                                String GAT_Name = (new String(ApduResp.DataOut, 0, 30, "UTF-16LE")).trim();

                                //Log.e("GJ","GAT_NAME="+GAT_Name);
                                String GAT_SexCode = (new String(ApduResp.DataOut, 30, 2, "UTF-16LE")).trim();
                                //Log.e("GJ","GAT_SexCode="+GAT_SexCode);
                                String ReserveA = (new String(ApduResp.DataOut, 32, 4, "UTF-16LE")).trim();
                                //Log.e("GJ","ReserveA="+ReserveA);
                                String GAT_Birthday = (new String(ApduResp.DataOut, 36, 16, "UTF-16LE")).trim();
                                //Log.e("GJ","GAT_Birthday="+GAT_Birthday);
                                String GAT_Address = (new String(ApduResp.DataOut, 52, 70, "UTF-16LE")).trim();
                                //Log.e("GJ","GAT_Address="+GAT_Address);
                                String GAT_IDNo = (new String(ApduResp.DataOut, 122, 36, "UTF-16LE")).trim();
                                //Log.e("GJ","GAT_IDNo="+GAT_IDNo);
                                String GAT_IssuingAuthority = (new String(ApduResp.DataOut, 158, 30, "UTF-16LE")).trim();
                                //Log.e("GJ","GAT_IssuingAuthority="+GAT_IssuingAuthority);
                                String GAT_ValidStartDate = (new String(ApduResp.DataOut, 188, 16, "UTF-16LE")).trim();
                                //Log.e("GJ","GAT_ValidStartDate="+GAT_ValidStartDate);
                                String GAT_ValidEndDate = (new String(ApduResp.DataOut, 204, 16, "UTF-16LE")).trim();
                                //Log.e("GJ","GAT_ValidEndDate="+GAT_ValidEndDate);
                                String GAT_PassportNo = (new String(ApduResp.DataOut, 220, 18, "UTF-16LE")).trim();
                                //Log.e("GJ","GAT_PassportNo="+GAT_PassportNo);
                                String GAT_IssuingNo = (new String(ApduResp.DataOut, 238, 4, "UTF-16LE")).trim();
                                //Log.e("GJ","GAT_IssuingNo="+GAT_IssuingNo);
                                String ReserveB = (new String(ApduResp.DataOut, 242, 6, "UTF-16LE")).trim();
                                //Log.e("GJ","ReserveB="+ReserveB);
                                String ReserveC = (new String(ApduResp.DataOut, 250, 6, "UTF-16LE")).trim();
                                //Log.e("GJ","ReserveC="+ReserveC);
                                System.arraycopy(ApduResp.DataOut, 256, recvBuf, 0, 1024);


                                String Gender;
                                String GAT_Sex;
                                if(GAT_SexCode.equals("1")) {
                                    Gender = "男";
                                    GAT_Sex = "男";
                                } else {
                                    Gender = "女";
                                    GAT_Sex = "女";
                                }


                                bean.fullName = GAT_Name;
                                bean.icon = decodePhoto(recvBuf);
                                bean.birthday = GAT_Birthday;
                                bean.idNo = GAT_IDNo;
                                bean.idOrg = GAT_IssuingAuthority;
                                bean.beginDate = GAT_ValidStartDate;
                                bean.endDate = GAT_ValidEndDate;
                                  bean.idAddr = GAT_Address;
//                                bean.countryCode= countryCode;
//                                bean.nation = countryMap.get(countryCode);
                                bean.sex = Gender;
                                bean.cardType = "2";
                                bean.reserveA = ReserveA;
                                bean.reserveB = ReserveB;
                                bean.reserveC = ReserveC;
                                bean.issuingNo= GAT_IssuingNo;
                                bean.passportNo= GAT_PassportNo;


							} else {

								for (myloopi = 0; myloopi < 15; myloopi++) {
									unicode = ((ApduResp.DataOut[myloopi * 2 + 1] & 0xff) << 8)
											| (ApduResp.DataOut[myloopi * 2 + 0] & 0xff);
									tmpstr += String.valueOf((char) unicode);
								}
								m_strName = tmpstr;
								// 性别
								tmpstr = "";
								for (myloopi = 0; myloopi < 1; myloopi++) {
									unicode = ((ApduResp.DataOut[30 + myloopi * 2 + 1] & 0xff) << 8)
											| (ApduResp.DataOut[30 + myloopi * 2 + 0] & 0xff);
									tmpstr += String.valueOf((char) unicode);
								}
								try {
									int sex_num = 9;
									if(tmpstr != null && tmpstr.trim().length() < 2){
										sex_num = Integer.parseInt(tmpstr);
									}
									tmpstr = findSexZW(sex_num);
								} catch (NumberFormatException e) {
									// 失败跳过再读取
									continue;

								}
								m_strGender = tmpstr;
								// 民族
								tmpstr = "";
								for (myloopi = 0; myloopi < 2; myloopi++) {
									unicode = ((ApduResp.DataOut[32 + myloopi * 2 + 1] & 0xff) << 8)
											| (ApduResp.DataOut[32 + myloopi * 2 + 0] & 0xff);
									tmpstr += String.valueOf((char) unicode);
								}
								try {
									int nation_num = Integer.parseInt(tmpstr);
									tmpstr = findMinzuZW(nation_num);
								} catch (NumberFormatException e) {
									// 失败跳过再读取
//									continue;
								}
								m_strNation = tmpstr;
								// 出身日期
								tmpstr = "";
								for (myloopi = 0; myloopi < 8; myloopi++) {

									unicode = ((ApduResp.DataOut[36 + myloopi * 2 + 1] & 0xff) << 8)
											| (ApduResp.DataOut[36 + myloopi * 2 + 0] & 0xff);
									tmpstr += String.valueOf((char) unicode);

								}
								m_strBornDate = tmpstr.substring(0, 4)
										+ tmpstr.substring(4, 6)
										+ tmpstr.substring(6, 8);
								// 详址
								tmpstr = "";
								for (myloopi = 0; myloopi < 35; myloopi++) {
									unicode = ((ApduResp.DataOut[52 + myloopi * 2 + 1] & 0xff) << 8)
											| (ApduResp.DataOut[52 + myloopi * 2 + 0] & 0xff);
									tmpstr += String.valueOf((char) unicode);
								}
								m_strAddress = tmpstr;
								// 身份证号码
								tmpstr = "";
								for (myloopi = 0; myloopi < 18; myloopi++) {
									unicode = ((ApduResp.DataOut[122 + myloopi * 2 + 1] & 0xff) << 8)
											| (ApduResp.DataOut[122 + myloopi * 2 + 0] & 0xff);
									tmpstr += String.valueOf((char) unicode);
								}
								m_strNumber = tmpstr;
								// 签发机关
								tmpstr = "";
								for (myloopi = 0; myloopi < 15; myloopi++) {
									unicode = ((ApduResp.DataOut[158 + myloopi * 2 + 1] & 0xff) << 8)
											| (ApduResp.DataOut[158 + myloopi * 2 + 0] & 0xff);
									tmpstr += String.valueOf((char) unicode);
								}
								m_strAuthor = tmpstr;
								// 有限期限
								tmpstr = "";
								for (myloopi = 0; myloopi < 16; myloopi++) {
									unicode = ((ApduResp.DataOut[188 + myloopi * 2 + 1] & 0xff) << 8)
											| (ApduResp.DataOut[188 + myloopi * 2 + 0] & 0xff);
									tmpstr += String.valueOf((char) unicode);
								}
								m_strPeriodFrom = tmpstr.substring(0, 8);
								try {
									m_strPeriodTo = tmpstr.substring(8, 16);
								} catch (ArrayIndexOutOfBoundsException e) {
									m_strPeriodTo = tmpstr.substring(8, 10);
								}
								System.arraycopy(ApduResp.DataOut, 256,
										recvBuf, 0, 1024);
								bean.fullName = m_strName;
								bean.icon = decodePhoto(recvBuf);
								if (bean.icon == null) {

									callback.onReadFail(
											ErrorDefine.ERR_IDCARD_IMAGE_PARSE,
											"身份证图片解析错误");
								}
								
								bean.birthday = m_strBornDate;
								bean.idNo = m_strNumber;
								bean.idOrg = m_strAuthor;
								bean.beginDate = m_strPeriodFrom;
								bean.endDate = m_strPeriodTo;
								bean.idAddr = m_strAddress;
								bean.nation = m_strNation;
								bean.sex = m_strGender;
								bean.cardType = "0";

//								if (withFinger) {
//									byte[] finger = new byte[1024];
//									int[] len = new int[1];
//									int res = api.IDCardFinger(finger, len);
//									if (res == 0) {
//										if (len[0] > 0) {
//											bean.fingerByte = finger;
//										} else {
//											bean.fingerByte = null;
//										}
//									}
//								}
							}
//							if (callback != null) {
//								callback.onReadSucc(bean);
//								callback = null;
//							}

							runFlag = false;
							i = 0;
//							api.IDCard_Close();
							break;
						}
						if (1 == ret) {
							if (i > 3 && callback != null) {
								callback.onReadFail(ErrorDefine.ERR_NO_IDCARD,
										"未放置身份证");
								i = 0;
							}
							i++;

						}
						if (2 == ret) {
							if (i > 3 && callback != null) {
								callback.onReadFail(ErrorDefine.ERR_NO_IDCARD,
										"未放置身份证");
								i = 0;
							}
							i++;

						}
						if (3 == ret) {
							if (i > 3 && callback != null) {
								callback.onReadFail(ErrorDefine.ERR_NO_IDCARD,
										"未放置身份证");
								i = 0;
							}
							i++;
						}
					}
				}
			} catch (UnsupportedEncodingException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			} finally {
				Log.e(TAG, "m_bThreadFinished = true");
				m_bThreadFinished = true;
				api.IDCard_Close();
				if (callback != null && bean.icon != null) {
					callback.onReadSucc(bean);
				}
				callback = null;
			}
		}

	}

	;

	private static String findSexZW(int sexcode) {
		if (sexcode == 0)
			return "未知";
		else if (sexcode == 1)
			return "男";
		else if (sexcode == 2)
			return "女";
		else if (sexcode == 9)
			return "未说明";
		return "未说明";
	}

	private static String[][] Nations = new String[][] { { "无" }, { "汉" },
			{ "蒙古" }, { "回" }, { "藏" }, { "维吾尔" }, { "苗" }, { "彝" }, { "壮" },
			{ "布依" }, { "朝鲜" }, { "满" }, { "侗" }, { "瑶" }, { "白" }, { "土家" },
			{ "哈尼" }, { "哈萨克" }, { "傣" }, { "黎" }, { "傈僳" }, { "佤" }, { "畲" },
			{ "高山" }, { "拉祜" }, { "水" }, { "东乡" }, { "纳西" }, { "景颇" },
			{ "柯尔克孜" }, { "土" }, { "达斡尔" }, { "仫佬" }, { "羌" }, { "布朗" },
			{ "撒拉" }, { "毛南" }, { "仡佬" }, { "锡伯" }, { "阿昌" }, { "普米" },
			{ "塔吉克" }, { "怒" }, { "乌孜别克" }, { "俄罗斯" }, { "鄂温克" }, { "德昂" },
			{ "保安" }, { "裕固" }, { "京" }, { "塔塔尔" }, { "独龙" }, { "鄂伦春" },
			{ "赫哲" }, { "门巴" }, { "珞巴" }, { "基诺" }, { "其他" }, { "外国血" } };

	private static String findMinzuZW(int minzucode) {
		if (minzucode > 59)
			return "";
		return Nations[minzucode][0];
	}

	public Bitmap decodePhoto(byte[] data) {
//		LogUtil.e(TAG,"头像数据:" + ByteTool.byte2hex(data));
		// 解码头像
//		Bitmap bmp = WltLib.parsePhotoOther(data);
//		Bitmap bmp = decodeIDImage(data);
		Bitmap bmp = Tool.getBitmap(data);
		return bmp;
	}
	
	final static HashMap<String, String> countryMap = new HashMap<String, String>();
	static{
		countryMap.put("AFG","阿富汗");
		countryMap.put("ALB","阿尔巴尼亚");
		countryMap.put("DZA",	"阿尔及利亚");
		countryMap.put("ASM", "美属萨摩亚");
		countryMap.put("AND",	"安道尔");
		countryMap.put("AGO",	"安哥拉");
		countryMap.put("AIA",	"安圭拉");
		countryMap.put("ATA",	"南极洲");
		countryMap.put("ATG",	"安提瓜和巴布达");
		countryMap.put("ARG",	"阿根廷");
		countryMap.put("ARM",	"亚美尼亚");
		countryMap.put("ABW", "阿鲁巴");
		countryMap.put("AUS",	"澳大利亚");
		countryMap.put("AUT",	"奥地利");
		countryMap.put("AZE",	"阿塞拜疆");
		countryMap.put("BHS",	"巴哈马");
		countryMap.put("BHR", "巴林");
		countryMap.put("BGD",	"孟加拉国");
		countryMap.put("BRB",	"巴巴多斯");
		countryMap.put("BLR",	"白俄罗斯");
		countryMap.put("BEL",	"比利时");
		countryMap.put("BLZ",	"伯利兹");
		countryMap.put("BEN",	"贝宁");
		countryMap.put("BMU", "百慕大");
		countryMap.put("BTN", "不丹");
		countryMap.put("BOL",	"玻利维亚");
		countryMap.put("BIH", "波黑");
		countryMap.put("BWA",	"博茨瓦纳");
		countryMap.put("BVT", "布维岛");
		countryMap.put("BRA", "巴西");
		countryMap.put("IOT", "英属印度洋领土");
		countryMap.put("BRN",	"文莱");
		countryMap.put("BGR",	"保加利亚");
		countryMap.put("BFA",	"布基纳法索");
		countryMap.put("BDI",	"布隆迪");
		countryMap.put("KHM","柬埔寨");
		countryMap.put("CMR","喀麦隆");
		countryMap.put("CAN","加拿大");
		countryMap.put("CPV","佛得角");
		countryMap.put("CYM","开曼群岛");
		countryMap.put("CAF","中非");
		countryMap.put("TCD","乍得");
		countryMap.put("CHL","智利");
		countryMap.put("CHN","中国");
		countryMap.put("HKG","香港");
		countryMap.put("MAC","澳门");
		countryMap.put("TWN","台湾");
		countryMap.put("CSR","圣诞岛");
		countryMap.put("CCK","科科斯(基林)群岛");
		countryMap.put("COL","哥伦比亚");
		countryMap.put("COM","科摩罗");
		countryMap.put("COG","刚果（布）");
		countryMap.put("COD","刚果（金）");
		countryMap.put("COK","库克群岛");
		countryMap.put("CR","哥斯达黎加");
		countryMap.put("CIV","科特迪瓦");
		countryMap.put("HRV","克罗地亚");
		countryMap.put("CUB","古巴");
		countryMap.put("CYP","塞浦路斯");
		countryMap.put("CZE","捷克");
		countryMap.put("DNK","丹麦");
		countryMap.put("DJI","吉布提");
		countryMap.put("DMA","多米尼克");
		countryMap.put("DOM","多米尼加共和国");
		countryMap.put("TMP","东帝汶");
		countryMap.put("ECU","厄瓜多尔");
		countryMap.put("EGY","埃及");
		countryMap.put("SLV","萨尔瓦多");
		countryMap.put("GNQ","赤道几内亚");
		countryMap.put("ERI","厄立特里亚");
		countryMap.put("EST","爱沙尼亚");
		countryMap.put("ETH","埃塞俄比亚");
		countryMap.put("FLK","福克兰群岛(马尔维纳斯)");
		countryMap.put("FRO","法罗群岛");
		countryMap.put("FJI","斐济");
		countryMap.put("FIN","芬兰");
		countryMap.put("FRA","法国");
		countryMap.put("GUF","法属圭亚那");
		countryMap.put("PYF","法属波利尼西亚");
		countryMap.put("ATF","法属南部领土");
		countryMap.put("GAB","加蓬");
		countryMap.put("GMB","冈比亚Gambia");
		countryMap.put("GEO","格鲁吉亚");
		countryMap.put("DEU","德国");
		countryMap.put("GHA","加纳");
		countryMap.put("GIB","直布罗陀");
		countryMap.put("GRC","希腊");
		countryMap.put("GRL","格陵兰");
		countryMap.put("GRD","格林纳达");
		countryMap.put("GLP","瓜德罗普");
		countryMap.put("GUM","关岛");
		countryMap.put("GTM","危地马拉");
		countryMap.put("GIN","几内亚");
		countryMap.put("GNB","几内亚比绍");
		countryMap.put("GUY","圭亚那");
		countryMap.put("HTI","海地");
		countryMap.put("HMD","赫德岛和麦克唐纳岛");
		countryMap.put("HND","洪都拉斯");
		countryMap.put("HUN","匈牙利");
		countryMap.put("ISL","冰岛");
		countryMap.put("IND","印度");
		countryMap.put("IDN","印度尼西亚");
		countryMap.put("IRN","伊朗");
		countryMap.put("IRQ","伊拉克");
		countryMap.put("IRL","爱尔兰");
		countryMap.put("ISR","以色列");
		countryMap.put("ITA","意大利");
		countryMap.put("JAM","牙买加");
		countryMap.put("JPN","日本");
		countryMap.put("JOR","约旦");
		countryMap.put("KAZ","哈萨克斯坦");
		countryMap.put("KEN","肯尼亚");
		countryMap.put("KIR","基里巴斯");
		countryMap.put("PRK","朝鲜");
		countryMap.put("KOR","韩国");
		countryMap.put("KWT","科威特");
		countryMap.put("KGZ","吉尔吉斯斯坦");
		countryMap.put("LAO","老挝");
		countryMap.put("LVA","拉脱维亚");
		countryMap.put("LBN","黎巴嫩");
		countryMap.put("LSO","莱索托");
		countryMap.put("LBR","利比里亚");
		countryMap.put("LBY","利比亚");
		countryMap.put("LIE","列支敦士登");
		countryMap.put("LTU","立陶宛");
		countryMap.put("LUX","卢森堡");
		countryMap.put("MKD","前南马其顿");
		countryMap.put("MDG","马达加斯加");
		countryMap.put("MWI","马拉维");
		countryMap.put("MYS","马来西亚");
		countryMap.put("MDV","马尔代夫");
		countryMap.put("MLI","马里");
		countryMap.put("MLT","马耳他");
		countryMap.put("MHL","马绍尔群岛");
		countryMap.put("MTQ","马提尼克");
		countryMap.put("MRT","毛里塔尼亚");
		countryMap.put("MUS","毛里求斯");
		countryMap.put("MYT","马约特");
		countryMap.put("MEX","墨西哥");
		countryMap.put("FSM","密克罗尼西亚联邦");
		countryMap.put("MDA","摩尔多瓦");
		countryMap.put("MCO","摩纳哥");
		countryMap.put("MNG","蒙古");
		countryMap.put("MSR","蒙特塞拉特");
		countryMap.put("MAR","摩洛哥");
		countryMap.put("MOZ","莫桑比克");
		countryMap.put("MMR","缅甸");
		countryMap.put("NAM","纳米比亚");
		countryMap.put("NRU","瑙鲁");
		countryMap.put("NPL","尼泊尔");
		countryMap.put("NLD","荷兰");
		countryMap.put("ANT","荷属安的列斯");
		countryMap.put("NCL","新喀里多尼亚");
		countryMap.put("NZL","新西兰");
		countryMap.put("NIC","尼加拉瓜");
		countryMap.put("NER","尼日尔");
		countryMap.put("NGA","尼日利亚");
		countryMap.put("NIU","纽埃");
		countryMap.put("NFK","诺福克岛");
		countryMap.put("MNP","北马里亚纳");
		countryMap.put("NOR","挪威");
		countryMap.put("OMN","阿曼");
		countryMap.put("PAK","巴基斯坦");
		countryMap.put("PLW","帕劳");
		countryMap.put("PST","巴勒斯坦");
		countryMap.put("PAN","巴拿马");
		countryMap.put("PNG","巴布亚新几内亚");
		countryMap.put("PRY","巴拉圭");
		countryMap.put("PER","秘鲁");
		countryMap.put("PHL","菲律宾");
		countryMap.put("PCN","皮特凯恩群岛");
		countryMap.put("POL","波兰");
		countryMap.put("PRT","葡萄牙");
		countryMap.put("PRI","波多黎各");
		countryMap.put("QAT","卡塔尔");
		countryMap.put("REU","留尼汪");
		countryMap.put("ROM","罗马尼亚");
		countryMap.put("RUS","俄罗斯联邦");
		countryMap.put("RWA","卢旺达");
		countryMap.put("SHN","圣赫勒拿");
		countryMap.put("KNA","圣基茨和尼维斯");
		countryMap.put("LCA","圣卢西亚");
		countryMap.put("SPM","圣皮埃尔和密克隆");
		countryMap.put("VCT","圣文森特和格林纳丁斯");
		countryMap.put("WSM","萨摩亚");
		countryMap.put("SMR","圣马力诺");
		countryMap.put("STp","圣多美和普林西比");
		countryMap.put("SAU","沙特阿拉伯");
		countryMap.put("SEN","塞内加尔");
		countryMap.put("SYC","塞舌尔");
		countryMap.put("SLE","塞拉利昂");
		countryMap.put("SGP","新加坡");
		countryMap.put("SVK","斯洛伐克");
		countryMap.put("SVN","斯洛文尼亚");
		countryMap.put("SLB","所罗门群岛");
		countryMap.put("SOM","索马里");
		countryMap.put("ZAF","南非");
		countryMap.put("SGS","南乔治亚岛和南桑德韦奇岛");
		countryMap.put("ESP","西班牙");
		countryMap.put("LKA","斯里兰卡");
		countryMap.put("SDN","苏丹");
		countryMap.put("SUR","苏里南");
		countryMap.put("SJM","斯瓦尔巴群岛");
		countryMap.put("SWZ","斯威士兰");
		countryMap.put("SWE","瑞典");
		countryMap.put("CHE","瑞士");
		countryMap.put("SYR","叙利亚");
		countryMap.put("TJK","塔吉克斯坦");
		countryMap.put("TZA","坦桑尼亚");
		countryMap.put("THA","泰国");
		countryMap.put("TGO","多哥");
		countryMap.put("TKL","托克劳");
		countryMap.put("TON","汤加");
		countryMap.put("TTO","特立尼达和多巴哥");
		countryMap.put("TUN","突尼斯");
		countryMap.put("TUR","土耳其");
		countryMap.put("TKM","土库曼斯坦");
		countryMap.put("TCA","特克斯科斯群岛");
		countryMap.put("TUV","图瓦卢");
		countryMap.put("UGA","乌干达");
		countryMap.put("UKR","乌克兰");
		countryMap.put("ARE","阿联酋");
		countryMap.put("GBR","英国");
		countryMap.put("USA","美国");
		countryMap.put("UMI","美国本土外小岛屿");
		countryMap.put("URY","乌拉圭");
		countryMap.put("UZB","乌兹别克斯坦");
		countryMap.put("VUT","瓦努阿图");
		countryMap.put("VAT","梵蒂冈");
		countryMap.put("VEN","委内瑞拉");
		countryMap.put("VNM","越南");
		countryMap.put("VGB","英属维尔京群岛");
		countryMap.put("VIR","美属维尔京群岛");
		countryMap.put("WLF","瓦利斯和富图纳");
		countryMap.put("ESH","西撒哈拉");
		countryMap.put("YEM","也门");
		countryMap.put("YUG","南斯拉夫");
		countryMap.put("ZMB","赞比亚");
		countryMap.put("ZWE","津巴布韦");
	}


	/**
	 * 解码身份证头像
	 * @param image
	 * @return
	 */
//	public Bitmap decodeIDImage(byte[] image) {
//		Bitmap bitmap = null;
//		try {
//			if (image == null)
//				return null;
//			byte[] buf = new byte[38556];
//			if (1 == IdCard.getimage(image, buf)) {
//				return Bgr2Bitmap(buf);
//			}
//			return null;
//
//		}
//		catch (Exception e) {
//
//			e.printStackTrace();
//
//			return bitmap;
//		}
//	}
//	public static Bitmap Bgr2Bitmap(byte[] bgrbuf) {
//		int width = 102;
//		int height = 126;
//		Bitmap bmp = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
//		int row = 0, col = width - 1;
//		for (int i = bgrbuf.length - 1; i >= 3; i -= 3) {
//			int color = bgrbuf[i] & 0xFF;
//			color += bgrbuf[i - 1] << 8 & 0xFF00;
//			color += bgrbuf[i - 2] << 16 & 0xFF0000;
//			bmp.setPixel(col--, row, color);
//			if (col < 0) {
//				col = width - 1;
//				row++;
//			}
//		}
//		return bmp;
//	}

}
